import store from '@/store'
import axios from 'axios'
// import router from '@/router'

// create an axios instance
const request = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // 设置超时时间
})

// 请求拦截器
request.interceptors.request.use(config => {
  // console.log(13, config)
  const token = store.state.user.token
  // 判断是否是员工页面，并且是新增员工
  // console.log(config)// 员工的路径：/employees
  // router.options.routes[12].path = '/employees'// 判断路径
  // console.log(config.method)// 员工的请求方法post
  // config.method = 'post'// 判断请求是否是新增
  // /auth/local/register
  if (token && config.url !== 'http://120.24.171.137:1337/auth/local/register') {
    // if (router.options.routes[12].path === '/employees' && config.method === 'post') {
    //   return config
    // } else {
    //   config.headers.Authorization = 'Bearer ' + token
    // }
    if (config.url === '/auth/local/register') {
      return config
    }
    config.headers.Authorization = 'Bearer ' + token
  }

  return config // 放行
})

// 相应拦截器
request.interceptors.response.use()

export default request
